package com.jiang.admin.core.thread;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.jiang.admin.core.thread.JobThread;

import java.util.concurrent.ConcurrentHashMap;

/**
 * 管理工作线程
 * @author jiangyougan 2018年12月2日 下午6:48:41
 *
 */
public class JobThreadManager {
	private static final Logger logger = LoggerFactory.getLogger(JobThreadManager.class);

	/**
	 *   任务线程
	 */
	private static ConcurrentHashMap<Integer, JobThread> jobThreadRepository = new ConcurrentHashMap<Integer, JobThread>();
	
	public static JobThread getJobThread(int jobId) {
		JobThread jobThread = jobThreadRepository.get(jobId);
		return jobThread;
	}

	public static JobThread registryJobThread(int jobId) {
		JobThread newJobThread = new JobThread(jobId);
		newJobThread.start();
		logger.info(">>>>>>>>>>> job registry JobThread success, jobId:{}", new Object[] {jobId});

		JobThread oldJobThread = jobThreadRepository.put(jobId, newJobThread);
		if (oldJobThread != null) {
			oldJobThread.toStop();
			oldJobThread.interrupt();
		}
		return newJobThread;
	}

	public static void removeJobThread(int jobId) {
		JobThread oldJobThread = jobThreadRepository.remove(jobId);
		if (oldJobThread != null) {
			oldJobThread.toStop();
			oldJobThread.interrupt();
		}
	}

}
